import * as d3 from 'd3'
import * as THREE from 'three'

function arrNomarlize(orgArr, change = [1, 1, 1]) {
  console.log('orgArr', orgArr)
  const xList = orgArr.map(ele => ele[0])
  const yList = orgArr.map(ele => ele[1])
  const zList = orgArr.map(ele => ele[2])
  let colors = []
  const xMid = d3.median(xList) // 取中位数
  const yMid = d3.median(yList)
  const zMid = d3.median(zList)
  console.log('x最大最小', d3.extent(xList)) // 显示最大最小值
  console.log('y最大最小',d3.extent(yList))
  console.log('z最大最小',d3.extent(zList))
  console.log('x最中位数',xMid)
  console.log('y最中位数',yMid)
  console.log('z最中位数',zMid)
 
  const vertices = orgArr.map((ele, idx) => {
    const x = (ele[0] - xMid) * change[0]
    const y = (ele[1] - yMid) * change[1]
    const z = (ele[2] - zMid) * change[2]
    return [x, y, z]
  })
  const yRealVal = vertices.map(ele => ele[1])
  const yColorScale = d3.scaleLinear().domain(d3.extent(yRealVal)).range([0.3,1])

  colors = vertices.map((ele, idx) => {
    return [yColorScale(ele[1]), 0, 0]
  })

  return {
    vertices: vertices.flat(),
    verticesPlane: vertices.map(ele => {
      const eleCopy = [...ele]
      eleCopy.splice(1,1)
      const res = new THREE.Vector2(...eleCopy)
      return res
    }),
    colors: colors.flat()
  }
  
}

export {
  arrNomarlize
}